<?php
jimport('joomla.application.component.model');
class svModelDonviquanly extends JModel
{
	//------------ tim kiem----------------------------------
	function getAllDonvi()
	{
		$query='SELECT * FROM qlcb_donviquanly WHERE 1';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getDonviByID($id)
	{
		$query='SELECT * FROM qlcb_donviquanly WHERE dvql_id='.$id;
		$result=$this->_loadObject($query);
		return $result;
	}
	function getCanboByIDs($idcanbo)
	{
		$query='SELECT  cb.cb_id,cb.cb_ten,cb.cb_namsinh,cb.cb_hinhanh,cb.cb_diachi,
		cv.cv_ten as chucvu,pb.pb_ten as phongban,cd.cd_ten as chucdanh
		FROM    qlcb_canbo as cb,qlcb_quanlychung as qlc,qlcb_phongban as pb,qlcb_chucvu as cv, qlcb_chucdanh as cd
		WHERE   cb.cb_id = qlc.qlc_idcanbo
		AND		qlc.qlc_idphongban=pb.pb_id
		AND		qlc.qlc_idchucvu=cv.cv_id
		AND		qlc.qlc_idchucdanh=cd.cd_id
		AND 	cb.cb_id='.$idcanbo;
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getPhongbanByDonvi($id)
	{
		$query='SELECT pb_id,pb_ten
				FROM qlcb_phongban
				WHERE pb_iddonviquanly='.$id;
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getTruongdonvi($id)
	{
		$query='SELECT  cb.cb_id,cb.cb_ten,cd.cd_ten as chucdanh
		FROM    qlcb_canbo as cb,qlcb_quanlychung as qlc,qlcb_chucvu as cv, qlcb_chucdanh as cd,qlcb_phongban as pb 
		WHERE   cb.cb_id = qlc.qlc_idcanbo
		AND		qlc.qlc_idchucvu=cv.cv_id
		AND     pb.pb_id=qlc.qlc_idphongban
		AND		pb.pb_iddonviquanly='.$id.'
		AND		qlc.qlc_idchucdanh=cd.cd_id
		AND 	cv.cv_id="1"';
		$result=$this->_loadObject($query);
		return $result;
	}
	function getPhodonvi($id)
	{
		$query='SELECT  cb.cb_id,cb.cb_ten,cd.cd_ten as chucdanh
		FROM    qlcb_canbo as cb,qlcb_quanlychung as qlc,qlcb_chucvu as cv, qlcb_chucdanh as cd,qlcb_phongban as pb
		WHERE   cb.cb_id = qlc.qlc_idcanbo
		AND		qlc.qlc_idchucvu=cv.cv_id
		AND     pb.pb_id=qlc.qlc_idphongban
		AND		pb.pb_iddonviquanly='.$id.'
		AND		qlc.qlc_idchucdanh=cd.cd_id
		AND 	cv.cv_ten="3"';
		$result=$this->_loadObject($query);
		return $result;
	}
	//-----------------------------------------------------------
	function getMaxIDCanbo()
	{
		$query='SELECT MAX(cb_id) as max
		FROM qlcb_canbo
		WHERE 1';
		$result=$this->_loadObject($query);
		return $result->max;
	}
	// cac ham phuc vu truy van CSDL
	function _loadObject($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObject();
		return $result;
	}
	function _loadObjectList($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObjectList();
		return $result;
	}
	function _Execute($query,$message)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		try {
			$db->query();
			return $message." thành công ";
		}
		catch (Exception $e){
			return $message." Không thành công! Vui lòng thử lại.";
		}
	}
}
?>